const redis = require('redis');

let client = null;

exports.connect = (config) => {
    client = redis.createClient({
        host: config.host,
        port: config.port,
        db: config.db
    });
    console.log('Connected to userRedis');
    client.on('error', (err) => {
        console.log('Error connecting to redis', err);
    })
}

exports.setUser = (userInfo) => {
    if (client == null) {
        throw new Error('Redis client not connected');
        return;
    }
    let key = "t_users" + "_" + userInfo.userid;
    client.hmset(key, userInfo, (err, reply) => {
        if (err) {
            console.log('Error setting user info', err);
        }
    });
    client.hgetall(key, (err, reply) => {
        if (err) {
            console.log('Error getting user info', err);
        }
        console.log(reply);
    })
}